package cn.com.gridinfo.sooc.core.my.msg.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.my.msg.adapter.NoticeMsgAdapter;
import cn.com.gridinfo.sooc.core.my.msg.dao.NoticeDao;

/**
 * Created by Michelle on 2016/1/6.
 * 消息公告
 */
public class NoticeMsgActivity extends BaseSoocActivity {
    @InjectView(R.id.msg_notice_listView)
    UltimateRecyclerView noticeRecycleView;
    private NoticeDao dao;
    NoticeMsgAdapter adapter;//适配器
    LinearLayoutManager linearLayoutManager;//列表管理器
    private Context context = NoticeMsgActivity.this;//上下文对象
    int maxLastPositon;//数据最后位置
    private int pageNo = 1;//记录消息公告当前页面
    private int pageSize = 10;//
    private int total_msg = 0;//消息总数
    private int total_page = 0; //总页数
    private String suid = null;
    List<HashMap<String, Object>> noticeMsgList = new ArrayList<HashMap<String, Object>>();//加载当前也面数据列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_msg_activity_notice);
        //获取用户
        suid = Arad.preferences.getString(LocalMark.SERVER_ID.getName().toString()) + "_"
                + Arad.preferences.getString(LocalMark.USER_UID.getName().toString());
        ButterKnife.inject(this);
        dao = new NoticeDao();
        noticeMsgList = dao.queryListByPage("suid='" + suid + "'", pageNo, pageSize);
        linearLayoutManager = new LinearLayoutManager(context);
        noticeRecycleView.setLayoutManager(linearLayoutManager);
        setNoticieMsgPage();
        setNoticeAll();
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "消息公告";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //点击课程分类
    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        finish();
    }

    /**
     * 消息公告分页
     */
    private void setNoticieMsgPage() {
        total_msg = dao.getTotalCount(suid);
        if (total_msg % pageSize > 0) {
            total_page = total_msg / pageSize + 1;
        } else {
            total_page = total_msg / pageSize;
        }

    }

    //设置消息公告数据数据
    public void setNoticeAll() {
        if (pageNo > total_page) {
            pageNo = total_page;//当前页是总页数
        } else {
            noticeMsgList = dao.queryListByPage("suid='" + suid + "'", pageNo, pageSize);
            //第一页,首次加载
            loadMessage();
        }
    }

    //初始化list数据 分页
    private void loadMessage() {
        Log.d("==============分页数据f=================");
        if (noticeMsgList.isEmpty()) {
            noticeRecycleView.showEmptyView();
        } else {
            adapter = new NoticeMsgAdapter(context, noticeMsgList);
            noticeRecycleView.setAdapter(adapter);
            noticeRecycleView.enableLoadmore();
            if (total_page > 1) {
                adapter.loadMoreDataView();
            }
            noticeRecycleView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    pageNo = 1;//首页
                    //加载首页数据
                    noticeMsgList = dao.queryListByPage("suid='" + suid + "'", pageNo, pageSize);
                    //刷新数据
                    adapter.refresh(noticeMsgList);
                    linearLayoutManager.scrollToPosition(0);
                    if (total_page > 1) {
                        adapter.loadMoreDataView();
                    }


                }
            });
            noticeRecycleView.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
                @Override
                public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {

                    if (pageNo < total_page) {
                        maxLastPositon = maxLastVisiblePosition;
                        //加载下一页数据
                        noticeMsgList = dao.queryListByPage("suid='" + suid + "'", ++pageNo, pageSize);
                        //加载更多
                        adapter.loadmore(noticeMsgList);
                    } else {
                        adapter.loadNoDataView();
                        linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                    }
                }
            });
        }
    }
}
